#include <stdio.h>
#include"huanshu.h"
#include"fu.h"
#include<time.h>
#include <stdlib.h>
#include <string.h>  
#define MEMBER_INFO_FILE "huiyuan.dat"
#define MEMBER_ID_BASE  4000

static unsigned int get_new_mid(void)
{
	member_info mi;
	unsigned int mid;

	FILE* fp = fopen(MEMBER_INFO_FILE, "rb");

	if(fp == NULL)
	{
		return MEMBER_ID_BASE;
	}

	fseek(fp, -sizeof(mi), SEEK_END);
	
	if(fread(&mi, sizeof(mi), 1, fp) == 1)
	{
		mid = mi.mid + 1;
	}
	else
	{
		mid = MEMBER_ID_BASE;
	}

	fclose(fp);

	return mid;
}



void print_member_info(const member_info* mi)
{
	printf("卡号：%s\n", mi->card_num);
	printf("会员ID：%u\n", mi->mid);
	printf("姓名：%s\n", mi->name);

	printf("手机号：%s\n", mi->phone);
	printf("密码：%s\n", mi->mima);
	printf("会员类型：");
	if(mi->mt == cika)
	{
		printf("次卡\n剩余次数：%d\n", mi->remain_cnt);
	}
	else if(mi->mt == yueka)
	{
		printf("月卡\n");
	}
	else if(mi->mt == nianka)
	{
		printf("年卡\n");	
	}

	struct tm* dt;

	dt = localtime(&mi->expire_time);

	printf("到期时间：%d年%d月%d日 %02d:%02d\n", dt->tm_year + 1900, dt->tm_mon + 1, dt->tm_mday, dt->tm_hour, dt->tm_min);

	dt = localtime(&mi->reg_time);

	printf("注册时间：%d年%d月%d日 %02d:%02d\n", dt->tm_year + 1900, dt->tm_mon + 1, dt->tm_mday, dt->tm_hour, dt->tm_min);
}


//申请

void member_reg(void)
{
	member_info mi;

	printf("请按照如下提示输入会员信息。\n\n");
	
	printf("卡号：");
	scanf("%s", mi.card_num);
	printf("姓名：");
	scanf("%s", mi.name);

	printf("手机号：");
	scanf("%s", mi.phone);
	printf("输入密码：");
	scanf("%s",mi.mima);
	printf("会员类型（1-次卡,2-月卡,3-年卡）：");
	scanf("%d", (int*)&mi.mt);

	if(mi.mt == cika)
	{
		printf("剩余次数：");
		scanf("%d", &mi.remain_cnt);
	}

	mi.reg_time = time(NULL);

	if(mi.mt == yueka)
		mi.expire_time = mi.reg_time + 30 * 24 * 60 * 60;
	else
		mi.expire_time = mi.reg_time + 365 * 24 * 60 * 60;

	mi.mid = get_new_mid();
	mi.delete_state = 0;

	FILE* fp = fopen(MEMBER_INFO_FILE, "ab");
	
	if(fp == NULL)
	{
		perror("会员注册失败");
		return;
	}

	fwrite(&mi, sizeof(mi), 1, fp);

	fclose(fp);

	printf("\n注册会员成功！\n");
}

//登陆刷卡

void member_show(void)
{
	member_info mi;
	char card_num[11];

	system("clear");  // 清屏

	printf("\n请刷卡！\n");
	scanf("%s",card_num);
	getchar();

	system("clear");

	FILE* fp = fopen(MEMBER_INFO_FILE, "rb");

	if(fp == NULL)
	{
		printf("\n卡片未注册，请联系工作人员处理！\n");
		return;
	}

	while(fread(&mi, sizeof(mi), 1, fp) == 1)
	{
		if(strcmp(mi.card_num, card_num) == 0)
			break;
	}

	if(feof(fp))
	{
		printf("\n卡片未注册，请联系工作人员处理！\n");
	}
	else
	{
		if(mi.delete_state == 1)
		{
			printf("\n此卡已注销，请联系工作人员处理！\n");
		}
		else
		{
			printf("\n尊贵的会员，您的个人信息如下：\n\n");
			print_member_info(&mi);
			
		}
		 
	}

	fclose(fp);
	
	press_any_key();
	system("clear");
}
//登陆手机号秘密

void member_show1(void)
{
	member_info mi;
	char card_num[12];
	char niu[12];
	system("clear");  // 清屏

	printf("\n请输入手机号！\n");
	scanf("%s",card_num);
	printf("\n请输入密码！\n");
	scanf("%s",niu);
	getchar();

	system("clear");

	FILE* fp = fopen(MEMBER_INFO_FILE, "rb");

	if(fp == NULL)
	{
		printf("\n请联系工作人员处理！\n");
		return;
	}

	while(fread(&mi, sizeof(mi), 1, fp) == 1)
	{
		if(strcmp(mi.phone, card_num) == 0 &&	 strcmp(mi.mima, niu) == 0)
			break;
	}

	if(feof(fp))
	{
		printf("\n卡片未注册，请联系工作人员处理！\n");
	}
	else
	{
		if(mi.delete_state == 1)
		{
			printf("\n此卡已注销，请联系工作人员处理！\n");
		}
		else
		{
			printf("\n尊贵的会员，您的个人信息如下：\n\n");
			print_member_info(&mi);
		}
	}

	fclose(fp);
	
	press_any_key();
	system("clear");
}


//删除
void member_del(void) {

    member_info mi;
    char card_num[11];
    char confirm;

    // 获取要删除的卡号
    system("clear");
    printf("\n请输入要删除的会员卡号：");
    scanf("%10s", card_num);
    getchar();

    // 打开原文件读取，临时文件写入
    FILE *fp = fopen(MEMBER_INFO_FILE, "rb");
    FILE *tmp = fopen("temp.dat", "wb");
    if (!fp || !tmp) {
        printf("\n文件操作失败！\n");
        if(fp) fclose(fp);
        if(tmp) fclose(tmp);
        press_any_key();
        return;
    }

    // 查找并删除
    int found = 0;
    while (fread(&mi, sizeof(mi), 1, fp)) {
        if (strcmp(mi.card_num, card_num) == 0) {
            found = 1;
            printf("\n找到会员：%s，确认删除？(Y): ", mi.name);
            confirm = getchar();
            getchar();
            
            if (confirm== 'y') {
                printf("\n已删除该会员记录！\n");
                continue; // 跳过写入，实现删除
            } else {
                printf("\n取消删除\n");
                fwrite(&mi, sizeof(mi), 1, tmp);
            }
        } else {
            fwrite(&mi, sizeof(mi), 1, tmp);
        }
    }

    fclose(fp);
    fclose(tmp);

    // 替换原文件
    if (found) {
        remove(MEMBER_INFO_FILE);
        rename("temp.dat", MEMBER_INFO_FILE);
    } else {
        printf("\n未找到该卡号！\n");
        remove("temp.dat");
    }

    press_any_key();
}


void chankan(void){

    FILE *fp = fopen(MEMBER_INFO_FILE, "rb");  // 以二进制读模式打开文件
    if (fp == NULL) {
        printf("暂无会员数据或文件打开失败！\n");
        return;
    }

    member_info mi;
    int count = 0;

    printf("\n====== 所有会员信息 ======\n");
    
    // 逐条读取并打印会员信息
    while (fread(&mi, sizeof(mi), 1, fp) == 1) {
        if (mi.delete_state == 0) {  // 只显示未删除的会员
            count++;
            printf("\n【会员 %d】\n", count);
            print_member_info(&mi);  // 调用现有的打印函数
            printf("-----------------------");
        }
    }

    if (count == 0) {
        printf("\n暂无有效会员数据！\n");
    } else {
        printf("\n\n共 %d 位会员\n", count);
    }

    fclose(fp);
}



